using System;

/// <summary>
/// Summary description for MyUtilities
/// </summary>
public class MyUtilities
{
    /// <summary>
    /// Kiem tra xem format so dien thoai co chinh xac hay khong.
    /// - Ky tu dau tien phai la so 0
    /// - Chuoi ko co dau space
    /// - Chuoi ko co dau '-'
    /// </summary>
    /// <example>084986114197</example>
    /// <example>0986114197</example>
    /// <remarks>0986-114-197</remarks>
    /// <param name="strMobile">Mobile Number</param>
    /// <param name="mobileLength">Do dai cua Mobile Number</param>
    /// <returns>
    ///     True: Neu thoa man format
    ///     False: Neu ko dung format
    /// </returns>
    public static bool CheckFormatMobile(string strMobile, int mobileLength)
    {
        try
        {
            if (strMobile.Trim() == "")
                throw new FormartMobileException(" So Mobile khong duoc de trong hoac Null!");
            if (strMobile.IndexOf('-') != -1)
                throw new FormartMobileException(strMobile + " : So Mobile khong co ky tu '-' ");
            if (strMobile.IndexOf(' ') != -1)
                throw new FormartMobileException(strMobile + " : So Mobile khong co dau cach (space) ");
            if (strMobile.IndexOf('/') != -1)
                throw new FormartMobileException(strMobile + " : So Mobile khong co dau '/' ");
            if (strMobile.IndexOf('_') != -1)
                throw new FormartMobileException(strMobile + " : So Mobile khong co dau '_') ");
            if (strMobile.Length != mobileLength)
                throw new FormartMobileException(strMobile + " : Do dai cua so Mobile phai = " + mobileLength);
            if (!strMobile.StartsWith("0"))
                throw new FormartMobileException(strMobile + " : So Mobile khong chinh xac. Ky tu dau tien phai la so 0");
            if (!IsDigit(strMobile))
                throw new FormartMobileException(strMobile + " : So Mobile khong chinh xac. Cac ky tu phai la kieu so.");
            return true;
        }
        catch (FormartMobileException ex)
        {
            throw new FormartMobileException("[" + ex.MyMessage + "]", ex);
        }
        catch (Exception ex)
        {
            throw new NumberFormatException("NumberFormatException : CheckFormatMobile", ex);
        }
    }

    /// <summary>
    /// Kiem tra xem chuoi str co phai la kieu so hay khong
    /// </summary>
    /// <param name="str">Chuoi kieu so</param>
    /// <returns>
    ///     True: Neu la kieu so
    ///     False: Neu ko phai la kieu so
    /// </returns>
    public static bool IsDigit(string str)
    {
        try
        {
            int.Parse(str);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// Convert string UTF8 to Base64
    /// </summary>
    /// <param name="str">string UTF8</param>
    /// <returns>
    ///     string Base64
    /// </returns>
    public static string ConvertToBase64(string str)
    {
        try
        {
            byte[] binaryData = new Byte[str.Length];
            binaryData = System.Text.Encoding.UTF8.GetBytes(str);
            str = Convert.ToBase64String(binaryData);
            return str;
        }
        catch (Exception ex)
        {
            LogFile.SaveErrLog(ex);
            throw new ConvertException("Convert To Base64 Error !",ex);
        }
    }
}
